/** decides a random event to occur when traveler travels within planets */
import java.util.Random;

import javax.swing.JOptionPane;



/**Constructor for TravelEvent
 * @author Ore ASonibare
 * @version 1.6
 */
public class TravelEvent {
       /**
        * Field i.
        */
       public  int i =0;

       /**
        * Field model.
        */
       public Model model=null;
       
       /**
        * Field view.
        */
       public View view=null;

       /**
        * Constructor for TravelEvent.
        * @param model Model
        * @param view View
        * @param i int
        */
       public TravelEvent(Model model, View view, int i){
		this.i=i;
		this.model = model;
		this.view = view;
		run(i);
	}
	
	/**
	 * Method toString.
	 * @return String
	 */
	public String toString(){
		return this.toString();
	}
	
	/**
	 * Method run.
	 * @param i int
	 */
	public final void run(int i){
		switch(i){
		case 0:rob();
		break;
		case 1:fuelDepletion();
		break;
		case 2:foundRandomGood();
		break;
		case 3:bribeCops();
		break;
		case 4:rats();
		break;
		default:rob();
		break;
		}
	}
	
	/**
	 * Method rob.
	 */
	public void rob(){
		final Goods[] goods = model.getShip().getGoodsArray();
		int mod = 0;
		for (int i=0;i < goods.length;i++){
			if (goods[i].getSold() > 4){
				goods[i].setSold(-2);
				view.sell[i].setText(goods[i].getSoldString());
				model.getShip().setCargo(model.getShip().getCargo() - 2 );
				view.setBay();
				mod++;
			}
		}
		if (mod > 0) {
		    JOptionPane.showMessageDialog(view, "You have been secretly " +
		    		"robbed of some goods by " +
		    		"space ninjas!");
		}
	}
	
	/**
	 * Method fuelDepletion.
	 */
	public void fuelDepletion(){
		final int fuel = model.getShip().getFuel();
		if (fuel > 3){
			JOptionPane.showMessageDialog(view, "A microscopic meteoroid " +
					"has collided with your " +
					"fuel tank.\n You have lost some extra fuel.");
			model.getShip().setFuel(fuel - 1 );
			view.setFuel();
		}
	}
	
	/**
	 * Method foundRandomGood.
	 */
	public void foundRandomGood(){
		 final Random randomGenerator = new Random();
		 final int randomInt = randomGenerator.nextInt(2);
		 final Goods[] goods = model.getShip().getGoodsArray();
		 final int x = 15 - model.getShip().getCargo();
		 if(x > 3) {
			 JOptionPane.showMessageDialog(view, "It's your lucky day" +
			 		"! You found some goods floating in space!");
		     goods[randomInt].setSold(2);
		     view.sell[randomInt].setText(goods[randomInt].getSoldString());
		     model.getShip().setCargo(model.getShip().getCargo() + 2);
		     view.setBay();
		 }
	}
	
	/**
	 * Method bribeCops.
	 */
	public void bribeCops(){
		if (model.getPlayer().getCredits() > 100){
			JOptionPane.showMessageDialog(view, "You were pulled over by " +
					"the Space Police for breaking the Law of Relativity.\n " +
					"Fortunately, you bribed your way out of jail time.");
			model.getPlayer().setCredit(model.getPlayer().getCredits() - 50);
			view.setCurrency();
		}
	}
	
	/**
	 * Method rats.
	 */
	public void rats(){
		final Goods[] goods = model.getShip().getGoodsArray();
		int mod = 0;
		for (int i=0;i < goods.length;i++){
			if (goods[i].getSold() > 4){
				goods[i].setSold(-2);
				view.sell[i].setText(goods[i].getSoldString());
				model.getShip().setCargo(model.getShip().getCargo() - 2 );
				view.setBay();
				mod++;
			}
		}
		if (mod > 0) {
		    JOptionPane.showMessageDialog(view, "Unfortunately space rats have " +
		    		"eaten some of your cargo.");
		}
		
		
	}
	
	
	
}
